package com.example.myapplication.Outpatientappointment;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.Penson.ModifyPasswordActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClientBean;
import com.example.myapplication.apter.ClientApter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    private Button btnExistingPatient;
    private Button but_bloal;
    private ImageView but_enteykesi;
    private TextView text_name;
    private TextView text_sex;
    private TextView text_sfz;
    private TextView text_phone;
    private TextView text_address;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private Gson gson;
    private Handler handler;
    private OkHttpClient okHttpClient;
    private Request request ;
    private RequestBody requestBody;
    private RadioGroup radioGroupGender;
    private RadioButton radioButtonMale;
    private RadioButton radioButtonFemale;
    private RecyclerView re_register;
    private ClientBean clientBean;
    private RadioGroup item_radioGroupGender011;
    private String sexName;
    private Button addegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initviewe();
        //返回
        butexid();
        //卡片列表
        generator();
        //点击添加就诊人
        butaddRegister();
    }

    private void butaddRegister() {

        but_enteykesi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addRegister();
            }
        });

        addegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addRegister();
            }
        });
    }

    //添加就诊人
    private  void addRegister() {
        AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
        View inflate = getLayoutInflater().inflate(R.layout.item_generator02, null);
        EditText ed_name = inflate.findViewById(R.id.ed_name);
        RadioGroup item_radioGroupGender = inflate.findViewById(R.id.item_radioGroupGender02);
        EditText ed_phone = inflate.findViewById(R.id.ed_phone);
        EditText ed_sfz = inflate.findViewById(R.id.ed_sfz);
        EditText ed_address = inflate.findViewById(R.id.ed_address);
        builder.setView(inflate);


        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //发送请求JSON请求,添加就诊人
                JSONObject jsonObject = new JSONObject();
                sexName = "";
                int checkedRadioButtonId = item_radioGroupGender.getCheckedRadioButtonId();
                if(checkedRadioButtonId==R.id.iten_radioButtonMale02){
                    sexName = "0";
                }else if(checkedRadioButtonId==R.id.item_radioButtonFemale02) {
                    sexName = "0";
                }else {
                }
                try {
                    jsonObject.put("name", ed_name.getText().toString());
                    jsonObject.put("sex", sexName);
                    jsonObject.put("cardId", ed_sfz.getText().toString());
                    jsonObject.put("tel", ed_phone.getText().toString());
                    jsonObject.put("address", ed_address.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                requestBody = RequestBody.create(jsonObject.toString(), JSON);
                request = new Request.Builder()
                        .url(TokenBean.IP + "/prod-api/api/hospital/patient")
                        .header("Authorization", TokenBean.token)
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegisterActivity.this.getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show();
                                generator();
                            }
                        });
                    }
                });

            }
        });
        builder.create().show();
    }

    private void butexid() {
        but_bloal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });
    }

    private void generator(){
        request = new Request.Builder()
                                    .url(TokenBean.IP+"/prod-api/api/hospital/patient/list")
                                    .header("Authorization",TokenBean.token)
                                    .build();
                            okHttpClient.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    String string = response.body().string();
                                    clientBean = gson.fromJson(string, ClientBean.class);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            getbeanDate();
                                        }
                                    }) ;
                                }
                            });

                        }

    private void getbeanDate() {
        ClientApter clientApter = new ClientApter(RegisterActivity.this,clientBean.getRows());
        re_register.setAdapter(clientApter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RegisterActivity.this);
        re_register.setLayoutManager(linearLayoutManager);
    }

    private void initviewe() {
        but_bloal = findViewById(R.id.but_bloal);
        addegister = findViewById(R.id.addegister);
        but_enteykesi = findViewById(R.id.but_enteykesi);
        re_register = findViewById(R.id.re_register);


        okHttpClient = new OkHttpClient();
        handler = new Handler();
        gson = new Gson();

    }
}